package com.business.system.api;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import com.business.system.bean.ApplyBean;
import com.business.system.bean.BackFeeLogBean;
import net.sf.rose.jdbc.DBUtils;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.util.DateFormat;
import net.sf.rose.web.utils.WebUtils;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.business.system.action.BaseAction;
import com.business.system.bean.GoodsOrdersBean;
import com.business.system.model.OrderModel;
import com.business.system.util.DBTools;

/**
 * @author: xudy
 * @date: 2018/04/12 8:58
 * @description:个人中心接口API
 */
@RestController
@RequestMapping("api/center")
public class ApiCenterAction extends BaseAction {

    /**
     * 我的代理和加盟清单
     * @param service
     * @param userId
     * @return
     */
    @RequestMapping("/myAgentAndJiameng.do")
    public List<Map<String,Object>> myAgentAndJiameng(Service service, String userId){
        //日期,类型,区域,品牌
        StringBuilder sql = new StringBuilder();
        sql.append("select t2.CREATE_TIME,t2.TYPE,t2.AREA,t3.BRAND_NAME from USER_BRAND t1");
        sql.append(" left join apply t2 on t1.apply_id=t2.id");
        sql.append(" left join brand t3 on t1.brand_id=t3.id");
        sql.append(" where 1=1 and t2.PAY_STATUS=3 and t1.USER_ID=?");
        List<Map<String,Object>> list = DBTools.getDataList(service,sql.toString(),userId);
        return list;
    }

    /**
     * 已完成推荐返利
     * t1.type=2 推荐返利
     * @param service
     * @param userId
     * @return
     */
    @RequestMapping("/recommendBackFinish.do")
    public List<Map<String,Object>> recommendBackFinish(Service service, HttpServletRequest request,String userId){
        Map<String,Object> params = WebUtils.getRequestData(request);
        StringBuilder sql = new StringBuilder();
        sql.append("select t3.USER_NAME,t2.APPLY_FEE,t1.UPDATE_TIME,t1.BACK_FEE from back_fee_log t1");
        sql.append(" left join apply t2 on t1.resource_id = t2.ID");
        sql.append(" left join wx_user t3 on t1.FROM_USER_ID = t3.ID where 1=1");
        sql.append(" and t1.user_id=?");
        sql.append(" and t1.type=? and t2.type=? and t2.PAY_STATUS=?");
        List<Object> condition = new ArrayList<>();
        condition.add(userId);
        condition.add(BackFeeLogBean.RECM);
        condition.add(ApplyBean.AGENT);
        condition.add(ApplyBean.PAY_IS_PASS);

        if(params.containsKey("month")){
            Date date = DateFormat.format(params.get("month").toString()+"-01 00:00:00",DateFormat.DATE_FORMAT_3);
            Date nextMonth = DateFormat.addMonth(date,1);
            sql.append(" and t1.create_time>=?");
            sql.append(" and t1.create_time<=?");
            condition.add(date);
            condition.add(nextMonth);
        }
        List<Map<String,Object>> result = DBTools.getDataList(service,sql.toString(),condition);
        return result;
    }

    /**
     * 未完成的推荐返利
     * @param service
     * @param request
     * @param userId
     * @return
     */
    @RequestMapping("/recommendBackTODO.do")
    public List<Map<String,Object>> recommendBackTODO(Service service,HttpServletRequest request,String userId){
        StringBuilder sql = new StringBuilder();
        sql.append("select t2.USER_NAME,t1.APPLY_FEE,t1.APPLY_FEE as BACK_FEE ,t1.PAY_STATUS,t1.APPLY_STATUS from apply t1");
        sql.append(" left join wx_user t2 on t1.USER_ID = t2.ID");
        sql.append(" where t2.FROM_ID=? and t1.PAY_STATUS<3 and t1.APPLY_STATUS<3");
        List<Map<String,Object>> list = DBTools.getDataList(service,sql.toString(),userId);
        return list;
    }

    /**
     * 已完成的代理返利
     * @param service
     * @param request
     * @param userId
     * @return
     */
    @RequestMapping("/agentBack.do")
    public List<Map<String,Object>> agentBack(Service service,HttpServletRequest request,String userId){
        StringBuilder sql = new StringBuilder();
        sql.append("select t3.USER_NAME,t2.TOTAL_FEE,t2.PAY_TIME,t1.BACK_FEE from back_fee_log t1");
        sql.append(" left join goods_orders t2 on t1.RESOURCE_ID = t2.ID");
        sql.append(" left join wx_user t3 on t1.FROM_USER_ID = t3.ID");
        sql.append(" where t1.USER_ID=?");
        List<Map<String,Object>> list = DBTools.getDataList(service,sql.toString(),userId);
        return list;
    }

    /**
     * 订单详细
     * @param service
     * @param userId
     * @param status 1:待支付,2:待发货,3:已发货,4:已收货,5:已取消
     * @return
     */
    @RequestMapping("/getOrderList.do")
    public List<OrderModel> getOrderList(Service service,String userId,String status){
        Map<String,Object> params = new HashMap<>();
        List<OrderModel> list = new ArrayList<>();
        params.put("userId",userId);
        params.put("status",status);
        //获取订单信息
        List<GoodsOrdersBean> orders = DBUtils.getBeanList(service,GoodsOrdersBean.class,params);
        if(orders!=null && orders.size()>0){
            for(GoodsOrdersBean order : orders){
                OrderModel orderModel = new OrderModel();
                orderModel.setOrders(order);
                orderModel.setGoodsList( orderService.getOrderGoodsById(service,order.getId()));
                list.add(orderModel);
            }
        }
        return list;
    }

    /**
     * 获取订单详细(包括物流信息)
     * @param service
     * @param orderId
     * @return
     */
    @RequestMapping("/getOrderDetail.do")
    public OrderModel getOrderDetail(Service service,String orderId){
        OrderModel model = new OrderModel();
        GoodsOrdersBean order = DBUtils.getBean(service,GoodsOrdersBean.class,orderId);
        model.setOrders(order);
        model.setGoodsList(orderService.getOrderGoodsById(service,orderId));
        return model ;
    }
}